from tortoise.models import Model
from tortoise import fields
from datetime import date


class CheckinRecord(Model):
    """打卡记录模型"""
    
    id = fields.IntField(pk=True, description="记录ID")
    checkin_date = fields.DateField(description="打卡日期")
    points_awarded = fields.IntField(default=0, description="本次打卡获得的积分")
    continuous_days = fields.IntField(default=1, description="打卡时的连续天数")
    created_at = fields.DatetimeField(auto_now_add=True, description="创建时间")
    
    # 外键关系
    user = fields.ForeignKeyField(
        "models.User", 
        related_name="checkin_records",
        on_delete=fields.CASCADE,
        description="关联用户"
    )
    
    class Meta:
        table = "checkin_records"
        table_description = "打卡记录表"
        ordering = ["-checkin_date"]  # 按打卡日期倒序
        unique_together = [("user", "checkin_date")]  # 用户每天只能打卡一次
    
    def __str__(self):
        return f"CheckinRecord(id={self.id}, user_id={self.user_id}, date={self.checkin_date})"